import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ApiUrl} from "../../../config/api-url";
import {FormBuilder, FormControl, FormGroup, ValidationErrors, Validators} from "@angular/forms";
import {Observable, Observer} from "rxjs";
import {BaseService} from "../../../shared/service/base.service";

@Component({
  selector: 'app-refund',
  templateUrl: './refund.component.html',
  styleUrls: ['./refund.component.less']
})
export class RefundComponent implements OnInit {
  // 模态框显隐
  @Input()
  isVisible = false;
  @Input()
  orderRefund: any;
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  // 提交时提交按钮是否加载中
  isOkLoading: any;
  // 模态框表单
  formModel: FormGroup;

  constructor(private fb: FormBuilder, public baseService: BaseService) {
  }

  ngOnInit(): void {
    this.buildForm();
  }

  buildForm() {
    this.formModel = this.fb.group({
      operatePassword: [null, [Validators.required], [this.passwordValidator]],
      refundReason: [null, [Validators.required]]
    });
  }

  /**
   * 密码验证
   * @param control
   */
  passwordValidator = (control: FormControl) =>
    new Observable((observer: Observer<ValidationErrors | null>) => {
      if (control.value.length < 6) {
        observer.next({duplicated: true, error: true});
        observer.complete();
      } else {
        observer.next(null);
        observer.complete();
      }
    });


  // 取消
  handleCancel() {
    this.isVisible = false;
    this.formModel?.reset({});
    this.emit.emit(this.isVisible);
  }

  // 确认
  handleOk() {
    if (this.baseService.zorro.validateFormValidate(this.formModel)) {
      this.isOkLoading = true;
      let body = {
        outTradeNo: this.orderRefund.outTradeNo,
        orderId: this.orderRefund.id,
        operatePassword: this.formModel.value.operatePassword,
        refundReason: this.formModel.value.refundReason,
      }
      this.baseService.post(ApiUrl.OrderRefund + '/refund', body).subscribe(res => {
        if (res.status == 0) {
          this.baseService.layer.messageSuccess("退款成功");
          this.handleCancel();
          this.isOkLoading = false;
        } else {
          this.baseService.layer.messageError(res.msg);
          this.isOkLoading = false;
        }
      });

    }
    // this.isOkLoading = true;
    // for (const i in this.formModel.controls) {
    //   if (!this.formModel.controls[i].valid) {
    //     this.isOkLoading = false;
    //   }
    //   this.formModel.controls[i].markAsDirty();
    //   this.formModel.controls[i].updateValueAndValidity();
    // }
    //
    // if (!this.isOkLoading) {
    //   return;
    // }
    // this.baseService.post(ApiUrl.UserUrl + '/register', this.formModel.value).subscribe(res => {
    //   if (res.status == 0) {
    //     this.baseService.layer.messageSuccess("注册成功");
    //     this.handleCancel();
    //     this.isOkLoading = false;
    //   } else {
    //     this.baseService.layer.messageError(res.msg);
    //     this.isOkLoading = false;
    //   }
    // });
  }

}
